# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description：菜单列表
"""
from fastapi import APIRouter

from core.Utils import build_tree
from models.base import Menu

from core.Response import base_response
from schemas.menu import MenuSchemas

menu_router = APIRouter(prefix="/menu")


@menu_router.get("", summary="菜单列表")
async def menu_list(type: str = None) -> dict:
    if type:
        if type != 'list' and type != 'tree':
            return base_response(code=400, msg="type参数错误")
    menus = await Menu.filter(level__gte=1).all()
    menus_list = [dict(MenuSchemas(**dict(menu))) for menu in menus]
    if type != 'tree':
        return base_response(code=200, msg="success", data=menus_list)
    else:
        tree = build_tree(menus_list)
        return base_response(code=200, msg="success", data=tree)

